package com.wye.controller;

import com.github.pagehelper.PageInfo;
import com.wye.goods.pojo.Brand;
import com.wye.service.IBrandService;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @GetMapping(value = "/category/{id}")
    public Result findByCategory(@PathVariable(value = "id") Integer categoryId){
        List<Brand> brandList = brandService.findByCategoryId(categoryId);
        return new Result(true, StatusCode.OK, "查询品牌成功", brandList);
    }
    /**
     * 查询所有品牌
     */
    @GetMapping("/findAll")
    public Result<List<Brand>> findAll(){
        List<Brand> brands = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌集合", brands);
    }

    @GetMapping("/findBrandById/{id}")
    public Result findBrandById(@PathVariable(value = "id")Integer id){
        Brand brand = brandService.findById(id);
        if (brand == null){
            return new Result(false, StatusCode.ERROR, "请求失败");
        }
        return new Result(true, StatusCode.OK, "请求成功", brand);
    }

    @PostMapping(value = "/addBrand")
    public Result addBrand(Brand brand){
        int resultInt = brandService.insertBrand(brand);
        if (resultInt != 0){
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @RequestMapping(value = "/updateBrand", method = RequestMethod.PUT)
    public Result updateBrand(@RequestBody Brand brand){
        brandService.updateBrand(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/deleteBrand/{id}", method = RequestMethod.DELETE)
    public Result deleteBrand(@PathVariable(value = "id") Integer id){
        brandService.deleteBrand(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping(value = "/search")
    public Result<List<Brand>> search(Brand brand){
        //调用service实现查询
        if (brand != null){
            List<Brand> brands = brandService.findList(brand);
            return new Result<List<Brand>>(true, StatusCode.OK, "查询成功", brands);
        }
        return new Result<List<Brand>>(false, StatusCode.ERROR, "请输入有效参数");
    }

    @RequestMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable(value = "page")Integer page, //当前页
                           @PathVariable(value = "size")Integer size,
                           Brand brand){//每页显示的条数
        if (brand == null){
            PageInfo<Brand> pageInfo = brandService.findPage(page, size);
            return new Result(true, StatusCode.OK, "分页查询成功", pageInfo);
        }
        PageInfo<Brand> pageInfo = brandService.findPage(page, size, brand);
        return new Result(true, StatusCode.OK, "分页搜索查询成功", pageInfo);

    }
    

}
